
var crypto = require('crypto');

// hash编码
var hash = crypto.createHash('md5');

hash.update(new Buffer('123', 'binary'));
var code = hash.digest('hex');
console.log(code);

hash = crypto.createHash('md5');
hash.update('123');
var code = hash.digest('hex');
console.log(code);

// hmac
var hmac = crypto.createHmac('md5', 'cai'); // 使用cai字符串作为私钥进行md5加密
hmac.update('123');
code = hmac.digest('hex');
console.log(code);
// 该部分的加密算法和hash的方法是类似的，只是在加密时添加了一个密码，确保加密字符的安全；

// Cipher | Decipher
var key = 'salt_form';
var plainText = 'baidu.com';
plainText = encodeURI(plainText);
var cipher = crypto.createCipher('aes-256-cbc', key); // 创建一个cipher加密对象，参数分别是：算法类型，私钥
var decipher = crypto.createDecipher('aes-256-cbc', key);

cipher.update(plainText, 'utf8', 'hex');
// 编码可为utf8, ascii或者binary, output_encoding指定已经加密内容的输出编码方式，可以为binary, base64或hex
var encryPwd = cipher.final('hex'); // 返回加密内容，输出编码为binary, ascii或utf8

decipher.update(encryPwd, 'hex', 'utf8');
console.log(encryPwd);

var decryPwd = decipher.final('utf8');
console.log(decryPwd);


